/**
 * 采购单据打印
 */
function printOrder(isPrintPreview){
	isPreview = isPrintPreview;
	$.messager.progress({
		title : '提示',
		text : '数据处理中，请稍后....'
	});
    var params = $("#purchaseOrderForm").serialize();  
    $.post("supplierPurchaseJSON!printOrder",params, printOrderBKProcess,"json");
}

/**
 * 采购单据打印回调
 */
function printOrderBKProcess(data){
	$.messager.progress('close'); 
	var response = data;

	var returnCode = response.returnCode;
	var returnMsg = response.message;
	if (returnCode == SUCCESS){		   
		//新打印方法
		pocureDocuments(response.returnValue,isPreview);
	} else {		
        $.messager.alert('错误', returnMsg, 'error');
    }
}
function copyOrder(){
	var info = "你确定复制此单据?";
	$.messager.confirm('复制单据', info, function(r){
		if (r){
	    	var url = "supplierPurchaseJSON!copyPurchaseOrder";
	    	var params=$("#purchaseOrderForm").serialize();  
	    	$.post(url,params, copyOrderBackProcess,"json");	
	    }
	});
}
function copyOrderBackProcess(data){
    var response = data;
	var returnCode = response.returnCode;

	if (returnCode != SUCCESS)
	    $.messager.alert('复制单据失败', response.message, 'error');
	else {
        $.messager.alert('复制单据成功', "复制单据成功,单据号 " + response.returnValue, 'info');
    }	
}
/**
 * 总部采购单据信息遍历
 * @param data
 * @param isPrintPreview
 * @returns
 */
function pocureDocuments(data,isPrintPreview) {
	// 单据时间
	var time = data.creationTime.substring(0, 10);
	// 经手人
	var auditorName = data.orderCounter;
	// 供应商名称
	var clientName = data.supplier;
	// 库房
	var storeName = data.storeName;
	// 单据号
	var id = data.id;
	// 备注信息
	var comment = data.comment;
	// 取循环次数(打印页数）
	var counts = parseInt(data.products.length / 19);
	// 取余数
	var end = data.products.length % 19;
	// 合计数量
	var amountTo = data.totalQuantity;
	// 合计金额
	var totalAmount = data.totalRecCost;
	// 判断采购单据类型
	var pocureType = data.typeS;
	// 单据总页数
	var pageNum = parseInt(counts) + 1;
	//遍历信息
	var info = "";
	//分页信息
	var pageInfo = "";
	for (i = 1; i <= pageNum; i++) {			
		if (i == pageNum) {
			//没有余数,并且是最后一页,则总计单起一页
			if(end == 0  && i == pageNum ){
				info += "<tr><td>总合计</td><td></td><td></td><td></td><td>"+amountTo+"</td><td></td><td>"+totalAmount+"</td></tr>\n" ;
				pageInfo += "<tr><td><b>共 " + pageNum +" 页   第 " + i + "页</b></td></tr>\n" ;				
			}else {
				var sumNumber = 0;
				var sum = 0;
				var infoEnd = data.products.slice(counts * 19, counts * 19 + end);
				for (y = 0; y < infoEnd.length; y++) {
					info += "<tr><td>" + infoEnd[y].year + infoEnd[y].brand
							+ infoEnd[y].quarter + "</td><td>"
							+ infoEnd[y].productCode + infoEnd[y].color
							+ "</td><td>" + infoEnd[y].category + "</td><td>"
							+ infoEnd[y].unit + "</td><td>" + infoEnd[y].quantity
							+ "</td><td>" + infoEnd[y].recCost
							+ "</td><td>" + infoEnd[y].totalRecCost
							+ "</td></tr>\n" 
					sumNumber += parseInt(infoEnd[y].quantity);
					sum += parseInt(infoEnd[y].totalRecCost);
					// 最后一行算合计
					if (y == infoEnd.length - 1) {
						info += "<tr><td>页小计</td><td></td><td></td><td></td><td>" + sumNumber +"</td><td></td><td>"+ sum + "</td></tr>\n" +
								"<tr><td>总合计</td><td></td><td></td><td></td><td>"+amountTo+"</td><td></td><td>"+totalAmount+"</td></tr>\n" ;
						pageInfo += "<tr><td><b>共 " + pageNum +" 页   第 " + i + "页</b></td></tr>\n" ;									
					}
				}							
			}	
		} else {
			var sumNumber = 0;//数量小计
			var sum = 0; // 金额小计
			var infoTwo = data.products.slice((i - 1) * 19, (i - 1) * 19 + 19);
			for (z = 0; z < infoTwo.length; z++) {
				info += "<tr><td>" + infoTwo[z].year + infoTwo[z].brand
						+ infoTwo[z].quarter + "</td><td>"
						+ infoTwo[z].productCode + infoTwo[z].color
						+ "</td><td>" + infoTwo[z].category + "</td><td>"
						+ infoTwo[z].unit + "</td><td>" + infoTwo[z].quantity
						+ "</td><td>" + infoTwo[z].recCost
						+ "</td><td>" + infoTwo[z].totalRecCost
						+ "</td></tr>\n + "
				sumNumber += parseInt(infoTwo[z].quantity);
				sum += parseInt(infoTwo[z].totalRecCost);
				// 最后一行算合计
				if (z == infoTwo.length - 1) {
					info += "<tr><td>页小计</td><td></td><td></td><td></td><td>" + sumNumber +"</td><td></td><td>\n"+ sum + "</td></tr>\n" ;
					pageInfo += "<tr><td><b>共 " + pageNum +" 页   第 " + i + "页</b></td></tr>\n" ;	
				}
			}				
		}
	}
	procurePrint(pageInfo,pocureType,storeName, time, auditorName, clientName,info,id,comment,isPrintPreview);
	
}
 
/**
 * 采购单据打印输出方法
 * @param pageNum
 * @param page
 * @param pocureType
 * @param storeName
 * @param time
 * @param auditorName
 * @param clientName
 * @param data
 * @param id
 * @param comment
 * @param isPrintPreview
 * @returns
 */
function procurePrint(pageInfo,pocureType,storeName,time, auditorName, clientName, data,id,comment,isPrintPreview) {

	LODOP.PRINT_INITA(5,1,1183,600,"千禧总部——采购单小票打印");
	LODOP. SET_SHOW_MODE("PREVIEW_IN_BROWSE",true);
	LODOP.SET_PRINT_PAGESIZE(1,2100,1400,"");
	LODOP.SET_PRINT_MODE("PRINT_NOCOLLATE",1);
	//头部信息
	LODOP.ADD_PRINT_TEXT(3,134,190,33,pocureType);
	LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);  
	LODOP.SET_PRINT_STYLEA(0, "LinkedItem", -1); 
	LODOP.SET_PRINT_STYLEA(0,"FontSize",19);
	LODOP.SET_PRINT_STYLEA(0,"Bold",1);
	LODOP.ADD_PRINT_TEXT(7,305,458,27," 千禧宝贝品牌儿童服饰连锁(www.qxbaby.com)");
	LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);  
	LODOP.SET_PRINT_STYLEA(0, "LinkedItem", -1); 
	//分页信息
	LODOP.ADD_PRINT_TABLE(11,678,116,23,"<style> table,td,th {border: 0px solid black; border-style: solid;border-collapse: collapse;font-size:13px;}  td{text-align:center;}</style>\n"
			+ "<table border='0'  ><tr><td><b>"+pageInfo+"</b> </td></tr></table>");	
	
	LODOP.SET_PRINT_STYLEA(0,"FontSize",13);
	LODOP.SET_PRINT_STYLEA(0,"Bold",1);
	LODOP.SET_PRINT_STYLEA(0,"FontSize",10);
	LODOP.ADD_PRINT_TEXT(44,35,758,20,"      日期： "+time+"     经手人：   "+auditorName+"           供应商:  "+clientName+"              库房：" +storeName);
	LODOP.SET_PRINT_STYLEA(0,"Bold",1);
	//下划线
	LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);  
	LODOP.SET_PRINT_STYLEA(0, "LinkedItem", -1); 
	LODOP.ADD_PRINT_LINE(65, 105, 65, 200, 0, 0);
	LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);  
	LODOP.SET_PRINT_STYLEA(0, "LinkedItem", -1); 
	LODOP.ADD_PRINT_LINE(65, 250, 65, 330, 0, 0);
	LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);  
	LODOP.SET_PRINT_STYLEA(0, "LinkedItem", -1);
	LODOP.ADD_PRINT_LINE(65, 400, 65, 540, 0, 0);
	LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);  
	LODOP.SET_PRINT_STYLEA(0, "LinkedItem", -1);
	LODOP.ADD_PRINT_LINE(65, 400, 65, 510, 0, 0);
	LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);  
	LODOP.SET_PRINT_STYLEA(0, "LinkedItem", -1);
	LODOP.ADD_PRINT_LINE(65, 580, 65, 650, 0, 0);
	LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);  
	LODOP.SET_PRINT_STYLEA(0, "LinkedItem", -1);

	LODOP.ADD_PRINT_TABLE(61,25,824,375,"<style> table,td,th {border: 1px solid black; border-style: solid;border-collapse: collapse;font-size:13px;}  td{text-align:center;}</style>\n"
			+ "<table border='1'  width='740px'><thead><tr><td style='width:160px;'>商品名称</td><td style='width:140px'>商品型号</td><td style='width:110px'>商品类型</td><td style='width:60px'>单位</td><td style='width:60px'>数量</td><td style='width:60px'>单价</td><td style='width:80px'>金额</td></tr></thead>\n"
			+data
			+ "</table>");		
	LODOP.ADD_PRINT_TEXT(440,31,746,20,"  单据号："+id+"        备注："+comment);
	LODOP.SET_PRINT_STYLEA(0,"Bold",1);
	LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);  
	LODOP.SET_PRINT_STYLEA(0, "LinkedItem", -1); 
	if(isPrintPreview){
		LODOP.PREVIEWA();
	}else{
		LODOP.PRINT();
	}

}